using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.IO;

namespace ImageScale
{
	public class ScaleClass
	{
		private ScaleType _scaleType;
		private int _percentageValue;
		private int _widthValue;
		private int _heightValue;

		public int PercentageValue
		{
			get 
			{ 
				return _percentageValue; 
			}
			set 
			{ 
				_percentageValue = value;
				_scaleType = ScaleType.Percentage;
			}
		}
		public int Width
		{
			get 
			{ 
				return _widthValue; 
			}
			set 
			{ 
				_widthValue = value;
				_scaleType = ScaleType.Absolute;
			}
		}
		public int Height
		{
			get { return _heightValue; }
			set 
			{ 
				_heightValue = value;
				_scaleType = ScaleType.Absolute;
			}
		}


		public ScaleClass()
		{
		}
		public ScaleClass(int percentage)
		{
			_scaleType = ScaleType.Percentage;
			_percentageValue = percentage;
		}
		public ScaleClass(int width, int height)
		{
			_scaleType = ScaleType.Absolute;
			_widthValue = width;
			_heightValue = height;
		}

		public Bitmap Scale(string imagePath)
		{
			Image srcImage = Image.FromFile(imagePath);
			if (_scaleType == ScaleType.Percentage)
			{
				double fractionalPercentage = (_percentageValue / 100.0);
				_widthValue = (int)(srcImage.Width * fractionalPercentage);
				_heightValue = (int)(srcImage.Height * fractionalPercentage);
			}

			Bitmap dstBitmap = new Bitmap(_widthValue, _heightValue, srcImage.PixelFormat);
            
			dstBitmap.SetResolution(srcImage.HorizontalResolution, srcImage.VerticalResolution);
			Graphics graphics = Graphics.FromImage(dstBitmap);
            graphics.InterpolationMode = InterpolationMode.Default;
			graphics.DrawImage(srcImage, new Rectangle(0, 0, _widthValue, _heightValue),
			new Rectangle(0, 0, srcImage.Width, srcImage.Height), GraphicsUnit.Pixel);
			graphics.Dispose();

			return dstBitmap;
		}

        public Bitmap Scale(Image srcImage)
        {
            if (_scaleType == ScaleType.Percentage)
            {
                double fractionalPercentage = (_percentageValue / 100.0);
                _widthValue = (int)(srcImage.Width * fractionalPercentage);
                _heightValue = (int)(srcImage.Height * fractionalPercentage);
            }

            Bitmap dstBitmap = new Bitmap(_widthValue, _heightValue, srcImage.PixelFormat);

            dstBitmap.SetResolution(srcImage.HorizontalResolution, srcImage.VerticalResolution);
            Graphics graphics = Graphics.FromImage(dstBitmap);
            graphics.InterpolationMode = InterpolationMode.Default;
            graphics.DrawImage(srcImage, new Rectangle(0, 0, _widthValue, _heightValue),
            new Rectangle(0, 0, srcImage.Width, srcImage.Height), GraphicsUnit.Pixel);
            graphics.Dispose();

            return dstBitmap;
        }

        public static long GetImageFileSize(Image srcImage, int percentage)
        {
            ScaleClass sc = new ScaleClass(percentage);
            Image outImage = sc.Scale(srcImage);
            MemoryStream ms = new MemoryStream();
            outImage.Save(ms, srcImage.RawFormat);
            return ms.Length;
        }
        public static long GetImageFileSize(Image srcImage, int width, int height)
        {
            ScaleClass sc = new ScaleClass(width, height);
            Image outImage = sc.Scale(srcImage);
            MemoryStream ms = new MemoryStream();
            outImage.Save(ms, srcImage.RawFormat);
            return ms.Length;
        }

    }
}
